package mx.vlabs.soapdroid.utils;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;

import mx.vlabs.soapdroid.dto.DTO;

public class Utils {
	
	public static String capitalizeFirstLetter(String arg){
		
		if(arg.length()>1){
			return arg.substring(0,1).toUpperCase()+arg.substring(1, arg.length());
		}else{
			return arg.toUpperCase();
		}
		
	}
	
	public static String uncapitalizeFirstLetter(String arg){
		
		if(arg.length()>1){
			return arg.substring(0,1).toLowerCase()+arg.substring(1, arg.length());
		}else{
			return arg.toUpperCase();
		}
		
	}
	
	public static Type getListType(Field listField){
		
		Type listType = listField.getGenericType();
		
		if (listType instanceof ParameterizedType) {
		    Type elementType = ((ParameterizedType) listType).getActualTypeArguments()[0];
		    
		    return elementType;
			
		}
		
		return null;
	}
	
	public static boolean isDTOListType(Field listField){
		
		Type listType = getListType(listField);
		
		return isDTOType((Class<?>) listType);
	}
	
	public static boolean isSubClassOf(Class<?> parent,Class<?> child){
		return parent.isAssignableFrom(child);
	}
	
	public static boolean isDTOType(Class<?> propClass){
		return isSubClassOf(DTO.class, propClass);
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static Object readPrimitiveValue(Type type,Object arg1){
		
		if(!getFieldType(type).equals(List.class)){
			
			if(getFieldType(type).equals(Boolean.class)){
				arg1 = Boolean.parseBoolean(arg1.toString());
			}else if(getFieldType(type).equals(Integer.class)){
				arg1 = Integer.parseInt(arg1.toString());
				
			}else if(getFieldType(type).equals(Long.class)){
				arg1 = Long.parseLong(arg1.toString());
			}else if(getFieldType(type).equals(Double.class)){
				arg1 = Double.parseDouble(arg1.toString());
			}else if(getFieldType(type).equals(Float.class)){
				arg1 = Float.parseFloat(arg1.toString());
			}else if(getFieldType(type).equals(Date.class)){
				
				XMLGregorianCalendar date = null;
				try {
					date = DatatypeFactory.newInstance().newXMLGregorianCalendar(arg1.toString());
					
					
					arg1 = (Date) new Date(date.toGregorianCalendar().getTimeInMillis());
					
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			
			}else{
				arg1 = ((SoapPrimitive) arg1).getValue();
			}
		}
		
		return arg1;
		
	}
	
	public static Class getFieldType(Type type){
		
		if(type.equals(int.class)||type.equals(Integer.class)){
			return Integer.class;
		}else if(type.equals(float.class)||type.equals(Float.class)){
			return Float.class;
		}else if(type.equals(double.class)||type.equals(Double.class)){
			return Double.class;
		}else if(type.equals(long.class)||type.equals(Long.class)){
			return Long.class;
		}else if(type.equals(boolean.class)||type.equals(Boolean.class)){
			return Boolean.class;
		}else if(type.equals(String.class)){
			return String.class;
		}else{
			//return PropertyInfo.OBJECT_CLASS;
			return (Class) type;
		}
	}

}
